home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / utility / pl4019ax.zip / SHELLWOR.PL < prev    next >
Text File  |  1991-11-13  |  802b  |  43 lines

  1. #; shellwords.pl
  2. #;
  3. #; Usage:
  4. #;    require 'shellwords.pl';
  5. #;    @words = &shellwords($line);
  6. #;    or
  7. #;    @words = &shellwords(@lines);
  8. #;    or
  9. #;    @words = &shellwords;        # defaults to $_ (and clobbers it)
  10.  
  11. sub shellwords {
  12.     package shellwords;
  13.     local($_) = join('', @_) if @_;
  14.     local(@words,$snippet,$field);
  15.  
  16.     s/^\s+//;
  17.     while ($_ ne '') {
  18.     $field = '';
  19.     for (;;) {
  20.         if (s/^"(([^"\\]+|\\[\\"])*)"//) {
  21.         ($snippet = $1) =~ s#\\(.)#$1#g;
  22.         }
  23.         elsif (s/^'(([^'\\]+|\\[\\'])*)'//) {
  24.         ($snippet = $1) =~ s#\\(.)#$1#g;
  25.         }
  26.         elsif (s/^\\(.)//) {
  27.         $snippet = $1;
  28.         }
  29.         elsif (s/^([^\s\\'"]+)//) {
  30.         $snippet = $1;
  31.         }
  32.         else {
  33.         s/^\s+//;
  34.         last;
  35.         }
  36.         $field .= $snippet;
  37.     }
  38.     push(@words, $field);
  39.     }
  40.     @words;
  41. }
  42. 1;
  43.